<div class={backdropClass}
     tabindex="-1"
     data-a11y-dialog-hide
></div>
<div class={contentsClass}
     role="dialog"
     aria-label={label || ''}
     ref:node
>
  <div class="modal-dialog-document" role="document" style="background: {background || '#000'};">
    <div class="modal-dialog-header" on:click="onClickHeader(event)">
      {#if title}
        <h1 class="modal-dialog-title">{title}</h1>
      {/if}
      <div class="close-dialog-button-wrapper">
        <button class="close-dialog-button focus-fix"
                data-a11y-dialog-hide aria-label="{intl.closeDialog}">
          <SvgIcon className="close-dialog-button-svg" href="#fa-times" />
        </button>
      </div>
    </div>
    <slot></slot>
  </div>
</div>
<Shortcut scope="modal-{id}" key="Backspace" on:pressed="closeDialog(id)"/>
<style>
  :global(.modal-dialog[aria-hidden='true']) {
    display: none;
  }
  :global(.modal-dialog) {
    position: fixed;
    z-index: 10000;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .modal-dialog-backdrop {
    position: fixed;
    z-index: 10010;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    background: rgba(51, 51, 51, 0.9);
  }
  .modal-dialog-backdrop.should-animate {
    transition: opacity 0.2s linear;
  }
  .modal-dialog-contents {
    z-index: 10020;
    padding: 0;
    border: 1px solid var(--main-border);
    border-radius: 2px;
    display: flex;
    flex-direction: row;
    max-height: calc(100% - 20px);
    max-width: calc(100% - 20px);
    flex: 0 1 580px;
  }
  .modal-dialog-contents.should-animate {
    transition: opacity 0.2s linear;
  }

  .modal-dialog-document {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 100%;
    flex: 1;
  }
  .modal-dialog-header {
    width: 100%;
    background: var(--nav-bg);
    display: flex;
    align-items: center;
  }
  .modal-dialog-title {
    color: var(--nav-text-color);
    padding: 2px 0 2px 10px;
    margin: 0;
    font-size: 1.5em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .close-dialog-button-wrapper {
    flex: 1;
    display: flex;
    justify-content: flex-end;
  }
  .close-dialog-button {
    padding: 0;
    background: none;
    border: none;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  :global(.close-dialog-button-svg) {
    padding: 10px;
    fill: var(--button-primary-text);
    width: 24px;
    height: 24px;
    flex: 1;
  }
  .muted-style .modal-dialog-header {
    background: var(--muted-modal-bg);
  }
  .muted-style .close-dialog-button:focus {
    outline: 2px solid var(--muted-modal-focus);
  }
  .muted-style .close-dialog-button:hover {
    background: var(--muted-modal-hover);
  }
  .muted-style.modal-dialog-contents {
    border: none;
  }
  :global(body.modal-open) {
    overflow-y: hidden;
  }

  @media(min-width: 480px) {
    /* On desktop, some dialogs look bad if they expand to fit all the way. So we shrink
       them to fit if shrinkWidthToFit is true.*/
    .modal-dialog-contents.shrink-width-to-fit {
      flex: none;
    }
  }

  @media (max-width: 320px) {
    .modal-dialog-title {
      font-size: 1.3em;
    }
    :global(.close-dialog-button-svg) {
      padding: 7px;
      width: 18px;
      height: 18px;
    }
  }

  @media (max-width: 240px) {
    .modal-dialog-contents {
      min-width: calc(100% - 20px);
    }
  }
</style>
<script>
  import Shortcut from '../../shortcut/Shortcut.html'
  import SvgIcon from '../../SvgIcon.html'
  import { A11yDialog } from '../../../_thirdparty/a11y-dialog/a11y-dialog.js'
  import { classname } from '../../../_utils/classname.js'
  import { on, emit } from '../../../_utils/eventBus.js'
  import {
    pushShortcutScope,
    popShortcutScope
  } from '../../../_utils/shortcuts.js'

  export default {
    oncreate () {
      const { id } = this.get()
      this.onPopState = this.onPopState.bind(this)
      const dialogElement = this.refs.node.parentElement
      this._a11yDialog = new A11yDialog(dialogElement)
      this._a11yDialog.on('hide', () => {
        document.body.classList.toggle('modal-open', false)
        this.fire('close')
        this._a11yDialog.destroy()
        emit('destroyDialog', id)
        requestAnimationFrame(() => document.body.removeChild(dialogElement))
      })
      on('showDialog', this, this.showDialog)
      on('closeDialog', this, this.closeDialog)
      pushShortcutScope(`modal-${id}`)
    },
    ondestroy () {
      window.removeEventListener('popstate', this.onPopState)
      const { statePopped, statePushed, id } = this.get()
      if (statePushed && !statePopped) {
        // If we weren't closed due to popstate, then pop state to ensure the correct history.
        window.history.back()
      }
      popShortcutScope(`modal-${id}`)
    },
    components: { Shortcut, SvgIcon },
    data: () => ({
      // don't animate if we're showing a modal dialog on top of another modal dialog. it looks ugly
      shouldAnimate: !process.browser || document.getElementsByClassName('modal-dialog').length < 2,
      fadedIn: false,
      muted: false,
      className: undefined,
      title: undefined,
      shrinkWidthToFit: false,
      clickHeaderToClose: false
    }),
    computed: {
      backdropClass: ({ fadedIn, shouldAnimate }) => {
        return classname(
          'modal-dialog-backdrop',
          !fadedIn && 'hidden',
          shouldAnimate && 'should-animate'
        )
      },
      contentsClass: ({ fadedIn, muted, shouldAnimate, shrinkWidthToFit, className }) => {
        return classname(
          'modal-dialog-contents',
          !fadedIn && 'hidden',
          muted && 'muted-style',
          shouldAnimate && 'should-animate',
          shrinkWidthToFit && 'shrink-width-to-fit',
          className
        )
      }
    },
    methods: {
      showDialog (otherId) {
        const { id } = this.get()
        if (otherId !== id) {
          return
        }
        // This setTimeout is dumb, but it fixes issues with modals opening other modals
        // due to the popstate/pushstate dance.
        setTimeout(() => {
          requestAnimationFrame(() => {
            window.addEventListener('popstate', this.onPopState)
            this.set({ statePushed: true })
            window.history.pushState({ modal: id }, null, location.href)
            document.body.classList.toggle('modal-open', true)
            this._a11yDialog.show()
            this.set({ fadedIn: true })
            this.fire('show')
            emit('dialogDidRender', id)
          })
        })
      },
      onPopState (event) {
        const { id } = this.get()
        if (!(event.state && event.state.modal === id)) {
          // If the new state is not us, just assume that we need to be closed.
          // This will only fail if modals are ever nested more than 2 levels deep.
          this.set({ statePopped: true })
          this.closeDialog(id)
        }
      },
      closeDialog (otherId) {
        const { id } = this.get()
        if (id !== otherId) {
          return
        }
        this._a11yDialog.hide()
      },
      onClickHeader (e) {
        if (this.get().clickHeaderToClose) {
          e.preventDefault()
          e.stopPropagation()
          this._a11yDialog.hide()
        }
      }
    }
  }
</script>
